# constructProj/jsonConfig.py
# -*-coding:utf-8 -*-
# @file       jsonConfig.py
# @author     yearnext
# @version    1.0.0
# @date       2021-11-19
# @brief      constructProj jsonConfig.py component
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class uLedWidget(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.uiInit()

        self.onColor  = [50,205,50]
        self.offColor = [190, 190, 190]

        # 灯闪烁
        self.blinkState = 0
        self.blinkCnt = 0
        self.onTime   = 0
        self.offTime  = 0

        self.blinkTimer = QTimer(self)
        self.blinkTimer.timeout.connect(self.blinkProcess)
        self.blinkTimer.stop()

    def uiInit(self):
        self.ledSize = 15
        self.ledColor = [190, 190, 190]
        self.updateLedStyle()

    # def setColor(self, r=190, g=190, b=190):
    #     self.ledColor[0] = r
    #     self.ledColor[1] = g
    #     self.ledColor[2] = b
    #     self.updateLedStyle()

    def updateLedStyle(self):
        head = 'QLabel{'
        border = 'border:1px solid grey;border-radius:{0}px;'.format(self.ledSize / 2)
        sizeLimit = 'min-width:{0}px;min-height:{0}px;'.format(self.ledSize)
        size = 'width:{0}px;height:{0}px;'.format(self.ledSize)
        font = 'font-family:\"Consolas\";'
        color = 'background:rgb({0},{1},{2});'.format(self.ledColor[0], self.ledColor[1], self.ledColor[2])
        end = '}'

        # styleList = [head, border, font, color, end]
        styleList = [head, border, sizeLimit, size, font, color, end]

        result = ''
        for i in styleList:
            result += i

        self.setStyleSheet(result)

        # print(result)
        return result

    def setSize(self, size):
        self.ledSize = size
        self.updateLedStyle()

    def setOnColor(self, on:list=[50,205,50]):
        self.onColor[0] = on[0]
        self.onColor[1] = on[1]
        self.onColor[2] = on[2]

    def setOffColor(self, off:list=[190, 190, 190]):
        self.offColor[0] = off[0]
        self.offColor[1] = off[1]
        self.offColor[2] = off[2]

    def setColor(self, on:list, off:list):
        self.setOn(on)
        self.setOffColor(off)

    def ledOn(self):
        self.ledColor[0] = self.onColor[0]
        self.ledColor[1] = self.onColor[1]
        self.ledColor[2] = self.onColor[2]
        self.updateLedStyle()

    def ledOff(self):
        self.ledColor[0] = self.offColor[0]
        self.ledColor[1] = self.offColor[1]
        self.ledColor[2] = self.offColor[2]
        self.updateLedStyle()

    def setOn(self):
        self.blinkStop()
        self.ledOn()

    def setOff(self):
        self.blinkStop()
        self.ledOff()

    # 设置指示灯闪烁
    def blink(self, onTime:int=500, offTime:int=500, cnt:int=-1):
        if onTime <= 0 or offTime <= 0:
            return

        self.blinkTimer.stop()

        self.blinkCnt = cnt
        self.onTime   = onTime
        self.offTime  = offTime

        self.setOn()
        self.blinkState = 1
        self.blinkTimer.start(self.onTime)

    def blinkStop(self):
        self.blinkState = 0
        self.blinkTimer.stop()

    # 刷新时间显示
    def blinkProcess(self):
        self.blinkTimer.stop()

        if self.blinkCnt == 0:
            self.blinkStop()

        if self.blinkState == 1:
            self.setOff()
            self.blinkState = 2
            self.blinkTimer.start(self.offTime)
        elif self.blinkState == 2:
            if self.blinkCnt > 0:
                self.blinkCnt -= 1

            if self.blinkCnt == -1 or self.blinkCnt > 0:
                self.setOn()
                self.blinkTimer.start(self.onTime)
                self.blinkState = 1
            else:
                self.blinkStop()
        else:
            self.blinkStop()
